/**
 * 设备device.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    var url = CONTEXT_PATH + '/device/deviceList.do';
    var deviceType = $('#deviceType_').val();
    if(deviceType){
        url = url + '?deviceType=' + deviceType;
    }
    $("#device_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        striped:true,
        pageList:[10,30,50,100],
        url: url,
        idField: 'id',
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'prName', title: '所属项目', fixed: true, width: 150},
                {field: 'deviceId', title: '设备编号', fixed: true, width: 150},
                {field: 'deName', title: '设备名称', fixed: true, width: 200},
                {field: 'type', title: '设备类型', fixed: true, align: 'center',  width: 70, formatter: function (val) {
                        if(val) {
                            if(val == "dust"){
                                return "扬尘检测";
                            }
                            if(val == "crane"){
                                return "起重机械";
                            }
                            if(val == "video"){
                                return "视频监控";
                            }
                        }
                    }},
                {field: 'createDate', title: '创建时间', width: 130}
            ]
        ],
        onSelect:function(rowIndex, rowData){
            // device_loadDesc(rowData);
        },
        onCheck:function(rowIndex, rowData){
            var row = $("#device_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#device_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#device_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#device_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            device_findList();
                        }
                    });
                }
                //添加扬尘按钮
                if("addDustBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/dustProCheckPoint/toDustProCheckPoint.do?source=deviceManage";
                        }
                    });
                }
                //添加起重机按钮
                if("addCraneBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/craneProject/toCraneProject.do?source=deviceManage";
                        }
                    });
                }
                //添加视频按钮
                if("addVideoBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/videoProject/toVideoProject.do?source=deviceManage";
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            device_edit();
                        }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            device_del();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            device_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除设备
 */
function device_del() {
    var row = $("#device_dataList").datagrid("getSelected");
    var checked = $("#device_dataList").datagrid("getChecked");
    var url = "";
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除设备", timeout: 3000, showType: "slide" });
        return;
    }
    if(!row.type || row.type == ""){
        $.messager.show({ title:'操作提示', msg:"发生错误，该设备没有类型！", timeout:3000, showType:"slide" });
        return;
    }
    if(row.type == "dust"){
        url = CONTEXT_PATH + "/dustProCheckPoint/delDustProCheckPoint.do?id="+ row.id;
    }
    if(row.type == "crane"){
        url = CONTEXT_PATH + "/craneProject/del.do?id=" + row.id;
    }
    if(row.type == "video"){
        url = CONTEXT_PATH + "/videoProject/delVideoProject.do?id=" + row.id;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.deName + ' 设备吗？', function (r) {
        if (r) {
            $.ajax({
                url: url,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#device_dataList").datagrid("reload", {
                        prName:$("#prName_").val(),
                        deName:$("#deName_").val(),
                        deviceId:$("#deviceId_").val(),
                        type:$("#type_").combobox("getValue")
                    });
                }
            })
        }
    });
}

/**
 * 修改设备
 * @returns {boolean}
 */
function device_edit(){
    var row = $("#device_dataList").datagrid("getSelected");
    var checked = $("#device_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title:'操作提示', msg:"请选择修改设备", timeout:3000, showType:"slide" });
        return;
    }
    if(!row.type || row.type == ""){
        $.messager.show({ title:'操作提示', msg:"发生错误，该设备没有类型！", timeout:3000, showType:"slide" });
        return;
    }
    if(row.type == "dust"){
        window.location.href = CONTEXT_PATH + "/dustProCheckPoint/toDustProCheckPoint.do?id="+ row.id + "&source=deviceManage";
    }
    if(row.type == "crane"){
        window.location.href = CONTEXT_PATH + "/craneProject/toCraneProject.do?cpId=" + row.id + "&source=deviceManage";
    }
    if(row.type == "video"){
        window.location.href = CONTEXT_PATH + "/videoProject/toVideoProject.do?id=" + row.id + "&source=deviceManage";
    }
}

/**
 * 查看设备
 */
function device_view(iconCls){
    var row = $("#device_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的设备详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(sbxq.id, sbxq.url + row.id + "&type=" + row.type, iconCls, sbxq.closable, sbxq.title);
}

//查询设备
function device_findList(){
    refreshPageNumber("device_dataList");
    $("#device_dataList").datagrid("reload",
            {
                prName:$("#prName_").val(),
                deName:$("#deName_").val(),
                deviceId:$("#deviceId_").val(),
                type:$("#type_").combobox("getValue")
            }
        );
    //清楚选中项
    $("#device_dataList").datagrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}